import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.Date;


public class DBTubeItem
{
	/**
	 * The title of this DBTube item
	 */
	private String title;
	
	/**
	 * The textual contents of this DBTube item
	 */
	private String content;
	
	/**
	 * One ore more related DBPedia items for extended background information
	 */
	private List<DBTubeResource> dbTubeResources = new ArrayList<DBTubeResource>(4);
	
	/**
	 * A youtube video relevant to the subject in the news.
	 */
	private URL youtubeVideo;
	
	/**
	 * The news category of this DBTube Item
	 */
	private String category;
	
	/**
	 * The publishing agent of the original content.
	 */
	private String agent;
	
	/**
	 * The textual location where this DBTubeItem is referenced
	 */
	private List<DBTubeResource> location = new ArrayList<DBTubeResource>();
	
	/**
	 * The date the News of the DBPedia item was published
	 */
	private Date date;
	
	/**
	 * Link to the original news.
	 */
	private URL newsSource;
	
	DBTubeItem(String title){
		this.setTitle(title);
	}
	
	DBTubeItem(){
		this("");
	}
	
	public String getTitle(){
		return title;
	}
	
	public String getContent(){
		return content;
	}
	
	public List<DBTubeResource> getDBPediaContent(){
		return dbTubeResources;
	}
	
	public URL getRelatedVideo(){
		return youtubeVideo;
	}
	
	public void setTitle(String newTitle){
		this.title = newTitle;
	}
	
	public void setContent(String newContent){
		this.content = newContent;
	}
	/**
	 * Add a DBPedia url and its related tag from DBPedia spotlight
	 * @param newItem
	 * @param tag
	 */
	public void addDBPediaContent(DBTubeResource resource){
		dbTubeResources.add(resource);
	}
	
	/**
	 * Set the related video to a new video.
	 * @param newVideo The new video to be related
	 */
	public void setRelatedVideo(URL newVideo){
		this.youtubeVideo = newVideo;
	}
	
	/**
	 * clear the dbpedia items and its related keywords
	 */
	public void clearDBPediaContent(){
		dbTubeResources.clear();
	}

	/**
	 * @param location the location to set, string only
	 */
	public void addLocation(DBTubeResource location)
	{
		this.location.add(location);
	}

	/**
	 * @return the location
	 */
	public List<DBTubeResource> getLocation()
	{
		ArrayList<DBTubeResource> unknown = new ArrayList<DBTubeResource>();
		unknown.add(new DBTubeResource("Unknown", null));
		return location != null ?  location : unknown;
	}

	/**
	 * @param catagory the category to set
	 */
	public void setCategory(String category)
	{
		this.category = category;
	}

	/**
	 * @return the category
	 */
	public String getCategory()
	{
		return category != null ? category : "unknown";
	}

	/**
	 * @param agent the agent to set
	 */
	public void setAgent(String agent)
	{
		this.agent = agent;
	}

	/**
	 * @return the agent
	 */
	public String getAgent()
	{
		if(agent == null || agent.equals(""))
			return "unknown";
		return agent;
	}

	/**
	 * @param date2 the date to set
	 */
	public void setDate(Date date2)
	{
		this.date = date2;
	}

	/**
	 * @return the date that the DBTube news item was published
	 */
	public Date getDate()
	{
		return date;
	}

	/**
	 * @param newsSource the newsSource to set
	 */
	public void setNewsSource(URL newsSource)
	{
		this.newsSource = newsSource;
	}

	/**
	 * @return the newsSource
	 */
	public URL getNewsSource()
	{
		return newsSource;
	}
	
	public String toString(){
		String result = "";
		result += this.getAgent() + "\n";
		result += this.getDate().toString() + "\n";
		result += this.getTitle() + "\n";
		result += this.getContent() + "\n";
		result += this.getNewsSource() + "\n";
		result += this.getRelatedVideo() + "\n";
		for (DBTubeResource dbTubeResource :  this.getDBPediaContent())
		{
			result += dbTubeResource.getKeyword() + ",";
		}
		result += "\n";
		return result;
	}
	
	public boolean validate(){
		return 	(this.getTitle() != null || !this.getTitle().equals("")) &&
				(this.getContent() != null || !this.getContent().equals("")) &&
				!this.dbTubeResources.isEmpty() &&
				this.youtubeVideo != null &&
				this.date != null;
	}
	
	public boolean equals(Object other){
		if(other instanceof DBTubeItem){
			DBTubeItem otherDBTubeItem = (DBTubeItem) other;
			return 	otherDBTubeItem.getDate().equals(this.getDate()) && 
					otherDBTubeItem.getTitle().equals(this.getTitle());
		}
		return false;
	}
}
